home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************
- File : macperl.c - Mac specific extensions
- Author : Matthias Neeracher & Tim Endres
- Started : 28May91 Language : MPW C
- Last : 17Jan93
-
- Copyright (c) 1991-93 Matthias Neeracher & Tim Endres
- *********************************************************************/
-
- #include <Types.h>
- #include <Resources.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <SegLoad.h>
- #include <StandardFile.h>
- #include <Lists.h>
- #include <Files.h>
- #include <Memory.h>
- #include <TFileSpec.h>
-
- /* Ugly hack since QuickDraw defines another invert */
- #define RESOLVE_MAC_CONFLICTS
-
- #include "EXTERN.h"
- #include "perl.h"
-
- void InitToolbox()
- {
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
- }
-
- static void CenterWindow(DialogPtr dlg)
- {
- Rect * screen;
- short hPos;
- short vPos;
-
- screen = &qd.screenBits.bounds;
- hPos = screen->right+screen->left-dlg->portRect.right >> 1;
- vPos = (screen->bottom-screen->top-dlg->portRect.bottom)/3;
- vPos += screen->top;
- MoveWindow(dlg, hPos, vPos, true);
- }
-
- static ControlHandle GetDlgCtrl(DialogPtr dlg, short item)
- {
- short kind;
- Handle hdl;
- Rect box;
-
- GetDItem(dlg, item, &kind, &hdl, &box);
- return (ControlHandle) hdl;
- }
-
- static void GetDlgText(DialogPtr dlg, short item, char * text)
- {
- getitext((Handle) GetDlgCtrl(dlg, item), text);
- }
-
- static void SetDlgText(DialogPtr dlg, short item, char * text)
- {
- setitext((Handle) GetDlgCtrl(dlg, item), text);
- }
-
- static void GetDlgRect(DialogPtr dlg, short item, Rect * r)
- {
- short kind;
- Handle hdl;
-
- GetDItem(dlg, item, &kind, &hdl, r);
- }
-
- static void FrameDlgRect(DialogPtr dlg, short item)
- {
- Rect r;
-
- GetDlgRect(dlg, item, &r);
- InsetRect(&r, -4, -4);
- PenSize(3, 3);
- FrameRoundRect(&r, 16, 16);
- PenSize(1,1);
- }
-
- #define TempPStr(cstr) ((StringPtr) memcpy(tmpPStr+1, cstr, *tmpPStr = strlen(cstr)), tmpPStr)
-
- double
- do_answer(arglast)
- int *arglast;
- {
- register STR **st = stack->ary_array;
- register int sp = arglast[0];
- int maxarg = arglast[2] - sp;
- char * prompt;
- short item;
- DialogPtr dlg;
- Str255 tmpPStr;
-
- if (maxarg > 4)
- fatal("answer() called with more than 4 arguments");
-
- prompt = (char*)str_get(st[++sp]);
-
- dlg = GetNewDialog((maxarg>1) ? 1999+maxarg : 2001, NULL, (WindowPtr)-1);
- InitCursor();
- SetDlgText(dlg, 5, prompt);
-
- if (maxarg>1)
- for (item = 1; item<maxarg; ++item) {
- prompt = (char*)str_get(st[++sp]);
- memcpy(tmpPStr+1, prompt, *tmpPStr = st[sp]->str_cur);
- SetCTitle(GetDlgCtrl(dlg, item), tmpPStr);
- }
- else
- SetCTitle(GetDlgCtrl(dlg, 1), "\pOK");
-
- CenterWindow(dlg);
- ShowWindow(dlg);
- SetPort(dlg);
- FrameDlgRect(dlg, ok);
- ModalDialog((ModalFilterProcPtr)0, &item);
- DisposDialog(dlg);
-
- return (maxarg>1) ? maxarg-item-1 : 0;
- }
-
- static char string_reply[256];
-
- STR * do_ask(arglast, maxarg)
- int *arglast;
- int maxarg;
- {
- register STR **st = stack->ary_array;
- register int sp = arglast[0];
- char * prompt;
- short item;
- DialogPtr dlg;
- STR * str;
-
- if (maxarg > 2)
- fatal("ask() called with more than 2 arguments");
-
- prompt = (char*)str_get(st[++sp]);
-
- dlg = GetNewDialog(2010, NULL, (WindowPtr)-1);
- InitCursor();
- SetDlgText(dlg, 3, prompt);
-
- if (maxarg == 2)
- SetDlgText(dlg, 4, (char*)str_get(st[++sp]));
- SelIText(dlg, 4, 0, 1024);
-
- InitCursor();
- CenterWindow(dlg);
- ShowWindow(dlg);
- SetPort(dlg);
- FrameDlgRect(dlg, ok);
- ModalDialog((ModalFilterProcPtr)0, &item);
- switch (item) {
- case ok:
- str = str_2mortal(Str_new(22,257));
- str->str_cur = 256;
- str->str_pok = 1;
- GetDlgText(dlg, 4, (StringPtr) str->str_ptr);
- str->str_cur = strlen(str->str_ptr);
- break;
- case cancel:
- break;
- }
- DisposDialog(dlg);
-
- return (item == ok) ? str : &str_undef;
- }
-
- static ListHandle picklist = NULL;
-
- #define SetCell(cell, row, column) { (cell).h = column; (cell).v = row; }
- #define ROW(cell) (cell).v
-
- pascal void
- MacListUpdate(myDialog, myItem)
- DialogPtr myDialog;
- short myItem;
- {
- Rect myrect;
- #pragma unused (myItem)
-
- LUpdate(myDialog->visRgn, picklist);
- myrect = (**(picklist)).rView;
- InsetRect(&myrect, -1, -1);
- FrameRect(&myrect);
- }
-
- pascal Boolean
- MacListFilter(myDialog, myEvent, myItem)
- DialogPtr myDialog;
- EventRecord *myEvent;
- short *myItem;
- {
- Rect listrect;
- short myascii;
- Handle myhandle;
- Point mypoint;
- short mytype;
- int activate;
-
- SetPort(myDialog);
- if (myEvent->what == keyDown) {
- myascii = myEvent->message % 256;
- if (myascii == '\015' || myascii == '\003') { /* This is return or enter... */
- *myItem = 1;
- return true;
- }
- }
- else if (myEvent->what == mouseDown) {
- mypoint = myEvent->where;
- GlobalToLocal(&mypoint);
- GetDItem(myDialog, 4, &mytype, &myhandle, &listrect);
- if (PtInRect(mypoint, &listrect) && picklist != NULL) {
- if (LClick(mypoint, (short)myEvent->modifiers, picklist)) {
- /* User double-clicked in cell... */
- *myItem = 1;
- return true;
- }
- }
- }
- else if (myEvent->what == activateEvt && picklist != NULL) {
- activate = (myEvent->modifiers & 0x01) != 0;
- LActivate((Boolean) activate, picklist);
- }
-
- return false;
- }
-
- STR *
- do_pick(arglast)
- int *arglast;
- {
- register STR **st = stack->ary_array;
- register int sp = arglast[0];
- int maxarg = arglast[2] - sp - 1;
- char * prompt;
- short itemHit;
- Boolean done;
- DialogPtr dlg;
- ListHandle mylist;
- Cell mycell;
- short mytype;
- Handle myhandle;
- Point cellsize;
- Rect listrect, dbounds;
- char * item;
-
- prompt = (char*)str_get(st[++sp]);
- InitCursor();
- dlg = GetNewDialog(2020, NULL, (WindowPtr)-1);
-
- SetDlgText(dlg, 3, prompt);
- GetDItem(dlg, 4, &mytype, &myhandle, &listrect);
- SetDItem(dlg, 4, mytype, (Handle)MacListUpdate, &listrect);
-
- SetPort(dlg);
- InsetRect(&listrect, 1, 1);
- SetRect(&dbounds, 0, 0, 1, maxarg);
- cellsize.h = (listrect.right - listrect.left);
- cellsize.v = 17;
-
- listrect.right -= 15;
-
- picklist = LNew(&listrect, &dbounds, cellsize, 0,
- dlg, true, false, false, true);
-
- mylist = picklist;
- LDoDraw(false, mylist);
-
- SetCell(mycell, 0, 0);
- for (; mycell.v<maxarg; ++mycell.v) {
- item = str_get(st[++sp]);
- LSetCell(item, st[sp]->str_cur, mycell, mylist);
- }
-
- LDoDraw(true, mylist);
- CenterWindow(dlg);
- ShowWindow(dlg);
-
- for (done=false; !done; ) {
- SetPort(dlg);
- FrameDlgRect(dlg, ok);
- ModalDialog(MacListFilter, &itemHit);
- switch (itemHit) {
- case ok:
- SetCell(mycell, 0, 0);
- done = true;
- if (!LGetSelect(true, &mycell, picklist))
- itemHit = cancel;
- break;
- case cancel:
- done = true;
- break;
- }
- } /* Modal Loop */
-
- SetPort(dlg);
-
- LDispose(mylist);
- picklist = NULL;
- DisposDialog(dlg);
-
- if (itemHit == ok)
- return str_smake(st[arglast[0]+mycell.v+2]);
- else
- return &str_undef;
- }
-
- void GetProgFile(int * outCnt, FSSpec * desc)
- {
- short message;
- short count;
- FCBPBRec fcb;
- CInfoPBRec info;
-
- fcb.ioNamePtr = &desc->name;
- fcb.ioRefNum = CurResFile();
- fcb.ioFCBIndx = 0;
-
- if (PBGetFCBInfoSync(&fcb))
- fatal("MacPerl can't find its own resource fork.\n");
-
- desc->vRefNum = fcb.ioFCBVRefNum;
- desc->parID = fcb.ioFCBParID;
-
- if (FSpCatInfo(desc, &info))
- fatal("MacPerl can't find itself.\n");
-
- if (info.hFileInfo.ioFlLgLen) {
- desc[1] = desc[0];
- *outCnt = 2; /* Use data fork */
- } else
- *outCnt = 1;
-
- desc += *outCnt;
-
- CountAppFiles(&message, &count);
-
- if (count) {
- int i;
- AppFile arg;
-
- for (i=0; i++<count; ) {
- GetAppFiles(i, &arg);
-
- if (!(*outCnt)++) {
- if (count>1)
- warn("I picked \"%P\" as your script.\n", arg.fName);
- if (arg.fType != 'TEXT')
- fatal("MacPerl can't do anything with a non-text script.\n");
- }
-
- WD2FSSpec(arg.vRefNum, arg.fName, desc++);
-
- if (*outCnt == 100) {
- warn("MacPerl ignored all files after the first 100.\n");
-
- break;
- }
- }
- } else if (*outCnt == 1) {
- Point wh;
- SFTypeList types;
- SFReply reply;
-
- wh.h = wh.v = 75;
- types[0] = 'TEXT';
-
- SFGetFile(wh, "", (FileFilterProcPtr) nil, 1, types, (DlgHookProcPtr) nil, &reply);
-
- if (!reply.good)
- fatal("MacPerl needs a program file to run.\n");
-
- WD2FSSpec(reply.vRefNum, reply.fName, desc);
-
- ++*outCnt;
- }
- }
-